Socket
Socket
Sign inDemoInstall

@npmcli/package-json

Package Overview
Dependencies
Maintainers
5
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@npmcli/package-json

Programmatic API to update package.json


Version published
Weekly downloads
4.7M
increased by1.88%
Maintainers
5
Weekly downloads
 
Created

What is @npmcli/package-json?

@npmcli/package-json is a Node.js library that provides utilities for reading, manipulating, and writing package.json files. It simplifies the process of handling package.json files programmatically, making it easier to manage dependencies, scripts, and other metadata in a Node.js project.

What are @npmcli/package-json's main functionalities?

Read package.json

This feature allows you to read the contents of a package.json file. The code sample demonstrates how to load the package.json file from the current working directory and log its contents.

const { PackageJson } = require('@npmcli/package-json');

async function readPackageJson() {
  const pkg = await PackageJson.load(process.cwd());
  console.log(pkg.content);
}

readPackageJson();

Update package.json

This feature allows you to update the contents of a package.json file. The code sample demonstrates how to change the version of the package and save the updated package.json file.

const { PackageJson } = require('@npmcli/package-json');

async function updatePackageJson() {
  const pkg = await PackageJson.load(process.cwd());
  pkg.update({ version: '1.0.1' });
  await pkg.save();
}

updatePackageJson();

Add dependency

This feature allows you to add a new dependency to the package.json file. The code sample demonstrates how to add a new package to the dependencies section and save the updated package.json file.

const { PackageJson } = require('@npmcli/package-json');

async function addDependency() {
  const pkg = await PackageJson.load(process.cwd());
  pkg.update({ dependencies: { 'new-package': '^1.0.0' } });
  await pkg.save();
}

addDependency();

Other packages similar to @npmcli/package-json

Keywords

FAQs

Package last updated on 03 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc